package com.example.scene;

import java.security.spec.MGF1ParameterSpec;
import java.util.ArrayList;
import java.util.Random;
import java.util.Timer;

import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.AlphaModifier;
import org.andengine.entity.modifier.LoopEntityModifier;
import org.andengine.entity.modifier.MoveModifier;
import org.andengine.entity.modifier.MoveXModifier;
import org.andengine.entity.modifier.MoveYModifier;
import org.andengine.entity.modifier.ParallelEntityModifier;
import org.andengine.entity.modifier.PathModifier;
import org.andengine.entity.modifier.PathModifier.Path;
import org.andengine.entity.modifier.RotationModifier;
import org.andengine.entity.modifier.ScaleModifier;
import org.andengine.entity.modifier.SequenceEntityModifier;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.sprite.TiledSprite;
import org.andengine.entity.text.Text;
import org.andengine.input.touch.controller.MultiTouch;
import org.andengine.util.color.Color;
import org.andengine.util.modifier.IModifier;

import ultility.dataString;

import android.util.Log;

import com.example.fruit.GameActivity;
import com.example.manager.LevelManager;
import com.example.manager.PauseableTimerHandler;
import com.example.manager.ResourcesManager;
import com.example.manager.SceneManager;
import com.example.touch.MultiTouchHandler;

public class RingLeaderLevel {
	
	static GameScene mScene;
	ResourcesManager resourcesManager;
	public static RingLeaderLevel INSTANCE = new RingLeaderLevel();
	
	public static RingLeaderLevel getSharedInstance(){			
		mScene = (GameScene) SceneManager.getInstance().getCurrentScene();	
		return INSTANCE;
	}
	
	int width, height;
	int x1, y1; int desBossx, desBoxy;
	public AnimatedSprite boss;
	public AnimatedSprite giftBox1,giftBox2,giftBox3;
	int i = 0;
	float time;
	public static boolean isTouchFinal = false;
	public static String str;
	public static String decode;
	public void goLevel1(){ 
		Log.d("pos", "level");
		LevelManager.endingLevel = false;
		resourcesManager = ResourcesManager.getInstance();
		width = GameActivity.CAMERA_WIDTH;
		height = GameActivity.CAMERA_HEIGHT;
		
		giftBox1 = new AnimatedSprite(0, 0, resourcesManager.mTiledTextureRegionRingLeadergiftbox1, resourcesManager.vbom);
		giftBox2 = new AnimatedSprite(0, 0, resourcesManager.mTiledTextureRegionRingLeadergiftbox1, resourcesManager.vbom);
		giftBox3 = new AnimatedSprite(0, 0, resourcesManager.mTiledTextureRegionRingLeadergiftbox1, resourcesManager.vbom);
		
		/*giftBox1.setPosition(-giftBox1.getWidth(), (height-giftBox1.getHeight())/2);
		giftBox2.setPosition(width+giftBox1.getWidth(), (height-giftBox1.getHeight())/2);
		Random ran = new Random();
		if(ran.nextInt()<=2)
		giftBox3.setPosition(width+giftBox1.getWidth(), (height-giftBox1.getHeight())/2);
		else
			giftBox3.setPosition(-giftBox1.getWidth(), (height-giftBox1.getHeight())/2);*/
	//	giftBox1.setPosition(0, 0);
		giftBox1.animate(100);
		giftBox2.animate(100);
		giftBox3.animate(100);
		RotationModifier rotate = new RotationModifier(1f, 0, 360);
		giftBox1.registerEntityModifier(new LoopEntityModifier(rotate));
		giftBox2.registerEntityModifier(new LoopEntityModifier(rotate));
		giftBox3.registerEntityModifier(new LoopEntityModifier(rotate));
		mScene.showGiftBox(giftBox1,giftBox2,giftBox3);
		/*mScene.attachChild(giftBox1);
		mScene.attachChild(giftBox2);				
		mScene.attachChild(giftBox3);				
		*/
	/*	final MoveXModifier moveX1 = new MoveXModifier(0.3f, -giftBox1.getWidth(), (width-giftBox1.getWidth())/2+100);
		final MoveXModifier moveX2 = new MoveXModifier(0.3f, width+giftBox2.getWidth(), (width-giftBox2.getWidth())/2-100);
		final MoveXModifier moveX11 = new MoveXModifier(0.2f, (width-giftBox1.getWidth())/2+100, (width-giftBox1.getWidth())/2);
		final MoveXModifier moveX22 = new MoveXModifier(0.2f, (width-giftBox2.getWidth())/2-100, (width-giftBox2.getWidth())/2);
		giftBox1.registerEntityModifier(new SequenceEntityModifier(moveX1,moveX11));
		giftBox2.registerEntityModifier(new SequenceEntityModifier(moveX2,moveX22));
	*/
		/*final Text txt = new Text(0, 0, resourcesManager.font, "Completed", resourcesManager.vbom);
		mScene.attachChild(txt);
		 */
		/*ScaleModifier scale = new ScaleModifier(0.5f, 3, 1);
		txt.setPosition((width-txt.getWidth())/2, (height-txt.getHeight())/2);
		txt.registerEntityModifier(scale);
		
		int x3 = (int) giftBox3.getX();
		int y3 = (int) giftBox3.getY();
		final MoveXModifier moveX3;
		final MoveXModifier moveX33;
		final ScaleModifier mod1 = new ScaleModifier(0.5f, 0.1f, 2.4f);
		final ScaleModifier mod2 = new ScaleModifier(0.4f, 2.4f, 1f);
		if(x3>0){
			moveX3 = new MoveXModifier(0.4f, x3, (width-giftBox3.getWidth())/2-200);
			moveX33 = new MoveXModifier(0.4f, (width-giftBox3.getWidth())/2-200, (width-giftBox3.getWidth())/2);			
		}
		else{
			moveX3 = new MoveXModifier(0.4f, x3, (width-giftBox3.getWidth())/+200);
			moveX33 = new MoveXModifier(0.4f, (width-giftBox3.getWidth())/+200, (width-giftBox3.getWidth())/2);
		}
		TimerHandler timer = new TimerHandler(0.6f, false,new ITimerCallback() {
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				Log.d("pos", "load");
				txt.setVisible(false);
				MoveXModifier moveX1 = new MoveXModifier(0.5f, -giftBox1.getWidth(), (width-giftBox1.getWidth())/2+100);
				MoveXModifier moveX2 = new MoveXModifier(0.5f, width+giftBox2.getWidth(), (width-giftBox2.getWidth())/2-100);
				MoveXModifier moveX11 = new MoveXModifier(0.4f, (width-giftBox1.getWidth())/2+100, (width-giftBox1.getWidth())/2-250);
				MoveXModifier moveX22 = new MoveXModifier(0.4f, (width-giftBox2.getWidth())/2-100, (width-giftBox2.getWidth())/2+250);
				
				giftBox1.registerEntityModifier(new SequenceEntityModifier(moveX1,moveX11));
				giftBox1.registerEntityModifier(new SequenceEntityModifier(mod1,mod2));
				//giftBox1.registerEntityModifier(new ParallelEntityModifier(new SequenceEntityModifier(mod1,mod2),new SequenceEntityModifier(moveX1,moveX11)));
				giftBox2.registerEntityModifier(new SequenceEntityModifier(moveX2,moveX22));
				giftBox2.registerEntityModifier(new SequenceEntityModifier(mod1,mod2));
				giftBox3.registerEntityModifier(new SequenceEntityModifier(moveX3,moveX33));
				giftBox3.registerEntityModifier(new SequenceEntityModifier(mod1,mod2));
			}
		});
		resourcesManager.engine.registerUpdateHandler(timer);
		*/
		
	/*	Text[] arrStr = new Text[str.length()];
		int distance = 10;
		for (int i = 1; i <= str.length(); i++) {
			
			String text = str.substring(0, i);
			Text txtStr = new Text(posX, posY, resourcesManager.font, text, resourcesManager.vbom);
			arrStr[i-1] = txtStr;
		}*/
		/*int posX = width/2-150;
		int posY = height-50;
		int in = 0; time = 0.1f;
		final ScaleModifier scale1 = new ScaleModifier(1.7f, 0.1f, 1.5f);
		final ScaleModifier scale2 = new ScaleModifier(1.5f, 1.5f, 1f);
		for (int i = str.length()-1; i >= 0; i--) {
			String text = str.substring(i, i+1);
			final Text txtStr = new Text(posX, posY, resourcesManager.font, text, resourcesManager.vbom);
			final MoveXModifier move = new MoveXModifier(3.2f, posX, width/2+a-in++*130);
			mScene.attachChild(txtStr);
			PauseableTimerHandler pau = new PauseableTimerHandler(time, false, new ITimerCallback() {
				
				@Override
				public void onTimePassed(TimerHandler pTimerHandler) {
					txtStr.registerEntityModifier(new ParallelEntityModifier(new SequenceEntityModifier(scale1,scale2),move));
					time += 2;
				}
			});
			mScene.registerUpdateByTimerHandler(pau);
		}
		mScene.showText();
		isTouchFinal = true;*/
		
		/*x1 = 50; y1 = 300;
		desBossx = width/2; desBoxy = 100; 
		boss = new AnimatedSprite(width/2, 0, resourcesManager.mTiledTextureRegionRingLeader, resourcesManager.vbom);
		giftBox3 = new AnimatedSprite(desBossx, desBoxy, resourcesManager.mTiledTextureRegionRingLeadergiftbox3, resourcesManager.vbom);
		
		boss.setScale(14); giftBox1.setScale(14);giftBox2.setScale(14);giftBox3.setScale(14);
		ScaleModifier scaleMod1 = new ScaleModifier(0.5f, 0.1f, 1.3f);
		ScaleModifier scaleMod2 = new ScaleModifier(0.2f, 1.3f, 1f);
		MoveYModifier moveMod = new MoveYModifier(0.5f, 0, desBoxy);
		AlphaModifier alphaMod = new AlphaModifier(0.5f, 0.1f, 1);
		
		boss.registerEntityModifier(new ParallelEntityModifier(new SequenceEntityModifier(scaleMod1,scaleMod2),moveMod,alphaMod)
		{
			@Override
			public void onModifierFinished(IModifier<IEntity> pModifier,
					IEntity pItem) {
				// TODO Auto-generated method stub
				super.onModifierFinished(pModifier, pItem);
				i++;
				if (i == 1) {
					Log.d("pos", "finish");
					mScene.attachChild(giftBox1);
					mScene.attachChild(giftBox2);
					mScene.attachChild(giftBox3);
					ScaleModifier scaleMod1 = new ScaleModifier(0.5f, 0.1f,
							1.3f);
					ScaleModifier scaleMod2 = new ScaleModifier(0.2f, 1.3f, 1f);
					MoveModifier moveMod1 = new MoveModifier(0.5f, desBossx,
							x1, desBoxy, y1);
					MoveModifier moveMod2 = new MoveModifier(0.5f, desBossx,
							x1 + 250, desBoxy, y1);
					MoveModifier moveMod3 = new MoveModifier(0.5f, desBossx,
							x1 + 550, desBoxy, y1);
					AlphaModifier alphaMod = new AlphaModifier(0.3f, 0.1f, 1);
					giftBox1.registerEntityModifier(new ParallelEntityModifier(
							new SequenceEntityModifier(scaleMod1, scaleMod2),
							moveMod1, alphaMod) {

					});
					giftBox2.registerEntityModifier(new ParallelEntityModifier(
							new SequenceEntityModifier(scaleMod1, scaleMod2),
							moveMod2, alphaMod) {

					});
					giftBox3.registerEntityModifier(new ParallelEntityModifier(
							new SequenceEntityModifier(scaleMod1, scaleMod2),
							moveMod3, alphaMod) {

					});
				}
			}			
		});
		*/
		//mScene.attachChild(boss);
		
	}
	
	private ArrayList<TiledSprite> getTiledSpriteByString(String str2) {
		ArrayList<TiledSprite> arr = new ArrayList<TiledSprite>();
		TiledSprite sprite;
		int width = 60; int height = 50;
		for (int i = 0; i < str2.length(); i++) {
			String letter = str2.substring(i, i+1);
			char[] ch = letter.toCharArray();
			switch (ch[0]) { 
			case 'A':
				sprite = new TiledSprite(0, 0, resourcesManager.mTiledTextureRegionA, resourcesManager.vbom);
				sprite.setWidth(width);
				sprite.setHeight(height);
				sprite.setUserData("A");
				arr.add(sprite);
				break;
			case 'B':
				sprite = new TiledSprite(0, 0, resourcesManager.mTiledTextureRegionB, resourcesManager.vbom);
				sprite.setWidth(width);sprite.setUserData("B");
				sprite.setHeight(height);arr.add(sprite);
				break;
			case 'C':
				sprite = new TiledSprite(0, 0, resourcesManager.mTiledTextureRegionC, resourcesManager.vbom);
				sprite.setWidth(width);sprite.setUserData("C");
				sprite.setHeight(height);arr.add(sprite);
				break;
			case 'D':
				sprite = new TiledSprite(0, 0, resourcesManager.mTiledTextureRegionD, resourcesManager.vbom);
				sprite.setWidth(width);sprite.setUserData("D");
				sprite.setHeight(height);arr.add(sprite);
				break;
			case 'E':
				sprite = new TiledSprite(0, 0, resourcesManager.mTiledTextureRegionE, resourcesManager.vbom);
				sprite.setWidth(width);sprite.setUserData("E");
				sprite.setHeight(height);arr.add(sprite);
				break;
			case 'F':
				sprite = new TiledSprite(0, 0, resourcesManager.mTiledTextureRegionF, resourcesManager.vbom);
				sprite.setWidth(width);sprite.setUserData("F");
				sprite.setHeight(height);arr.add(sprite);
				break;
			case 'G':
				sprite = new TiledSprite(0, 0, resourcesManager.mTiledTextureRegionG, resourcesManager.vbom);
				sprite.setWidth(width);sprite.setUserData("G");
				sprite.setHeight(height);arr.add(sprite);
				break;
			case 'H':
				sprite = new TiledSprite(0, 0, resourcesManager.mTiledTextureRegionH, resourcesManager.vbom);
				sprite.setWidth(width);sprite.setUserData("H");
				sprite.setHeight(height);arr.add(sprite);
				break;
			case 'I':
				sprite = new TiledSprite(0, 0, resourcesManager.mTiledTextureRegionI, resourcesManager.vbom);
				sprite.setWidth(width);sprite.setUserData("I");
				sprite.setHeight(height);arr.add(sprite);
				break;
			case 'J':
				sprite = new TiledSprite(0, 0, resourcesManager.mTiledTextureRegionJ, resourcesManager.vbom);
				sprite.setWidth(width);sprite.setUserData("J");
				sprite.setHeight(height);arr.add(sprite);
				break;
			case 'K':
				sprite = new TiledSprite(0, 0, resourcesManager.mTiledTextureRegionK, resourcesManager.vbom);
				sprite.setWidth(width);sprite.setUserData("K");
				sprite.setHeight(height);arr.add(sprite);
				break;
			case 'M':
				sprite = new TiledSprite(0, 0, resourcesManager.mTiledTextureRegionM, resourcesManager.vbom);
				sprite.setWidth(width);sprite.setUserData("M");
				sprite.setHeight(height);arr.add(sprite);
				break;
			case 'N':
				sprite = new TiledSprite(0, 0, resourcesManager.mTiledTextureRegionN, resourcesManager.vbom);
				sprite.setWidth(width);sprite.setUserData("N");
				sprite.setHeight(height);arr.add(sprite);
				break;

			default:
				break;
			}
		}
		return arr;
	}
	ArrayList<TiledSprite> tiledSpriteText = null;
	PauseableTimerHandler time1;
	float dd;
	public void textSpecial(AnimatedSprite b)
	{
		removeGiftBox();
		miss = 0; length = 0;
		Log.d("pos", "text special");
		str = "AAA"; decode = "000";
	//isTouchFinal = true;	
		final int a = str.length()/2;
		final int ind = 0; final int dis = 70;
		tiledSpriteText = getTiledSpriteByString(str);
		final ScaleModifier scale1 = new ScaleModifier(0.5f, 0.1f, 1.4f);
		final ScaleModifier scale2 = new ScaleModifier(0.3f, 1.4f, 1f);
		final ArrayList<PauseableTimerHandler> arrTimer = new ArrayList<PauseableTimerHandler>();
		final float[] dur = {0.5f,0.8f,1.1f,1.4f,1.7f,2.0f};
	//	int inde = 0;
		for (int i=0;i<tiledSpriteText.size();i++) {
			final TiledSprite tiledSprite = tiledSpriteText.get(i);
			tiledSprite.setCurrentTileIndex(0);
			if(i<a){
				tiledSprite.setPosition(width/2-(a-i)*dis, height-100);
			}else{
				tiledSprite.setPosition(width/2+(i-a)*dis, height-100);
			}
			//ind++;			
			mScene.attachChild(tiledSprite); //Random ran = new Random();
			//tiledSprite.setVisible(false);
			tiledSprite.setAlpha(0);
		}
		dd = 0.1f;
		i = 0;
		time1 = new PauseableTimerHandler(dd, true, new ITimerCallback() {
			
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
			//	for (int i=0;i<tiledSpriteText.size();i++) 
				if(i<tiledSpriteText.size()){
				{
					final TiledSprite tiledSprite = tiledSpriteText.get(i++);
				//	tiledSprite.setVisible(true);
					PauseableTimerHandler p = new PauseableTimerHandler(0.1f, false, new ITimerCallback() {
						
						@Override
						public void onTimePassed(TimerHandler pTimerHandler) {
							final ScaleModifier mod1 = new ScaleModifier(0.8f, 0.1f, 1.5f);
							final ScaleModifier mod2 = new ScaleModifier(0.6f, 1.5f, 1f);
							final AlphaModifier alpha = new AlphaModifier(1.4f, 0.1f, 1);
							//tiledSprite.registerEntityModifier(new ParallelEntityModifier(new SequenceEntityModifier(mod1,mod2),new SequenceEntityModifier(scale1,scale2),alpha));
							tiledSprite.registerEntityModifier(new SequenceEntityModifier(mod1,mod2));
						//	tiledSprite.registerEntityModifier(new SequenceEntityModifier(scale1,scale2));
							tiledSprite.registerEntityModifier(alpha);
							Log.d("pos1", dur+"..");							
						}
					});
					mScene.registerUpdateHandler(p);
					dd += 0.15;
					time1.setTimerSeconds(dd);
				
					if(i==tiledSpriteText.size()-1){
						time1.setAutoReset(false);
						Log.d("pos", "cuoi");
						PauseableTimerHandler p1 = new PauseableTimerHandler(0.8f, false, new ITimerCallback() {
							
							@Override
							public void onTimePassed(TimerHandler pTimerHandler) {
								//isTouchFinal = true;
								mScene.showText(str);	
							}
						});
						mScene.registerUpdateHandler(p1);
					}		
			}
				}
			}});
			mScene.registerUpdateHandler(time1);
		
		/*new Runnable() {
			public void run() {
				for (PauseableTimerHandler pauseableTimerHandler : arrTimer) {
					mScene.registerUpdateHandler(pauseableTimerHandler);
				}
			}
		}.run();
		*/
		/*ArrayList<Point>arr = new ArrayList<RingLeaderLevel.Point>();
		ArrayList<Point>path = new ArrayList<RingLeaderLevel.Point>();
		ArrayList<Point>arr1 = new ArrayList<RingLeaderLevel.Point>();
		ArrayList<Point>path1 = new ArrayList<RingLeaderLevel.Point>();
		
		if(b.getX()==giftBox1.getX()){
			//arr = pathGiftBox1();
			Point p = new Point(giftBox1.getX(), giftBox1.getY());
			arr.add(p);
			int xc = 46;
			int yc = 45;
			p = new Point(giftBox1.getX()+xc*1, giftBox1.getY()-yc*1);
			arr.add(p);
			p = new Point(giftBox1.getX()+xc*2, giftBox1.getY()-yc*2);
			arr.add(p);
			p = new Point(giftBox1.getX()+xc*3, giftBox1.getY()-yc*3);
			arr.add(p);
			p = new Point(giftBox1.getX()+xc*4, giftBox1.getY()-yc*4);
			arr.add(p);
			
			arr1.add(p);
			xc = (int) p.x; yc = (int) p.y;
			int xcc = 7; int ycc = 95;
			p = new Point(xc+xcc*1, yc+ycc*1);
			arr1.add(p);
			p = new Point(xc+xcc*2, yc+ycc*2);
			arr1.add(p);
			p = new Point(xc+xcc*3, yc+ycc*3);
			arr1.add(p);
			p = new Point(xc+xcc*4, yc+ycc*4);
			arr1.add(p);
			
			smooth(arr, path);
			smooth(arr1, path1);
			
			final Text txt = new Text(b.getX(), b.getY(), resourcesManager.font, "Hello", resourcesManager.vbom);
			mScene.attachChild(txt);
			float[] coordX = new float[path.size()];
			float[] coordY = new float[path.size()];
			float[] coordX1 = new float[path1.size()];
			float[] coordY1 = new float[path1.size()];
			
			for(int i=0;i<path.size();i++){
				Point p1 = path.get(i);
				coordX[i] = p1.x;
				coordY[i] = p1.y;
			}for(int i=0;i<path1.size();i++){
				Point p1 = path1.get(i);
				coordX1[i] = p1.x;
				coordY1[i] = p1.y;
			}
			Path pathText = new Path(coordX, coordY);
			Path pathText1 = new Path(coordX1, coordY1);
			
			txt.registerEntityModifier(new SequenceEntityModifier(new PathModifier(1f, pathText),
					new PathModifier(1f, pathText1)));
			*/
			/*for (Point p : arr) {
				Log.d("pos",p.x+":"+p.y);
			}
			for (Point p : path) {
				Log.d("pos",p.x+":"+p.y);
			}*/
		
		
	}
	private void removeGiftBox() {
		giftBox1.clearEntityModifiers();giftBox2.clearEntityModifiers();giftBox3.clearEntityModifiers();
		giftBox1.clearUpdateHandlers();giftBox2.clearUpdateHandlers();giftBox3.clearUpdateHandlers();
		giftBox1.setVisible(false);giftBox2.setVisible(false);giftBox3.setVisible(false);
		giftBox1.detachSelf();giftBox2.detachSelf();giftBox3.detachSelf();
		
	}

	static Point sP;
	private ArrayList<Point> pathGiftBox1() {
		ArrayList<Point>arr = new ArrayList<RingLeaderLevel.Point>();
		Point p = new Point(giftBox1.getX(), giftBox1.getY());
		arr.add(p);
		sP = p;
		while (sP.x<width/2-50 && sP.y>50){
			sP.x += 15; sP.y -= 13;
			Point p1 = new Point(sP.x, sP.y);
			arr.add(p1);
		}
		while (sP.y<height){
			sP.x += 1; sP.y += 10;
			Point p1 = new Point(sP.x, sP.y);
			arr.add(p1);
		}
		for (Point p1 : arr) {
			Log.d("pos",p1.x+"::"+p1.y);
		}
		return arr;
	}
	public void smooth(ArrayList<Point> input, ArrayList<Point> output) {
		//expected size
		output.clear();
		output.ensureCapacity(input.size()*2);

		//first element
		output.add(input.get(0));
		//average elements
		for (int i=0; i<input.size()-1; i++) {
			Point p0 = input.get(i);
			Point p1 = input.get(i+1);

			Point Q = new Point(0.75f * p0.x + 0.25f * p1.x, 0.75f * p0.y + 0.25f * p1.y);
			Point R = new Point(0.25f * p0.x + 0.75f * p1.x, 0.25f * p0.y + 0.75f * p1.y);
	        output.add(Q);
		    output.add(R);
		}
		
		//last element
		output.add(input.get(input.size()-1));
		
	}
	public class Point
	{
		public float x;
		public float y;	
		public Point(float x,float y){
			this.x=x;
			this.y=y;
		}
	}
	String x = "X";
	public static int miss = 0;
	public static int length = 0;Text text = null;
	public void processTag(String tag) {
		for (final TiledSprite sprite : tiledSpriteText) {			
			if(((String)sprite.getUserData()).equalsIgnoreCase(tag) && sprite.getCurrentTileIndex()==0){
				length++;
				AlphaModifier mod = new AlphaModifier(0.3f, 1, 0);
				sprite.registerEntityModifier(mod);
				sprite.setCurrentTileIndex(1);	
				mScene.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() {					
					@Override
					public void onTimePassed(TimerHandler pTimerHandler) {						
						sprite.setAlpha(0f);
						final ScaleModifier mod1 = new ScaleModifier(0.3f, 0.1f, 1.5f);
						final ScaleModifier mod2 = new ScaleModifier(0.2f, 1.5f, 1f);
						final AlphaModifier alpha = new AlphaModifier(0.4f, 0.1f, 1);
						sprite.registerEntityModifier(new ParallelEntityModifier(new SequenceEntityModifier(mod1,mod2),alpha));
					}
				}));
				if(RingLeaderLevel.str.length()==RingLeaderLevel.length){
					mScene.pauseFinalLevel();
					RingLeaderLevel.getSharedInstance().goLevel2("pass");
				}
				//sprite.setUserData(sprite.getUserData()+"SET");
				return;
			}
		}
		
		if(miss==0){
			miss++;
			text = new Text(0, 0, resourcesManager.font, x+miss, resourcesManager.vbom);
			text.setPosition(30, -text.getHeight());
			text.setColor(Color.RED);
			MoveYModifier moveY = new MoveYModifier(0.6f, -text.getHeight(), 300);
			text.registerEntityModifier(moveY);			
			mScene.attachChild(text);
		}
		else{miss++;
			//text = new Text(30, 300, resourcesManager.font, x+miss, resourcesManager.vbom);
			text.setText(x+miss);
			final ScaleModifier mod1 = new ScaleModifier(0.3f, 0.1f, 1.5f);
			final ScaleModifier mod2 = new ScaleModifier(0.2f, 1.5f, 1f);
			
			text.registerEntityModifier(new SequenceEntityModifier(mod1,mod2));
			//mScene.attachChild(text);
		}
		if(RingLeaderLevel.miss>=3){
			mScene.pauseFinalLevel();
			RingLeaderLevel.getSharedInstance().goLevel2("fail");
		}
		if(RingLeaderLevel.str.length()==RingLeaderLevel.length){
			mScene.pauseFinalLevel();
			RingLeaderLevel.getSharedInstance().goLevel2("pass");
		}
	}
	Text txt1;
	Text txt;	
	public void goLevel2(String string) {
		//mScene.currentLevel++;
		isTouchFinal = false;
		miss = 0; length = 0;
		if (string.equalsIgnoreCase("fail")) {
			txt = new Text(0, 0, resourcesManager.font,
					"Try again later!", resourcesManager.vbom);			
		}
		else if(string.equalsIgnoreCase("pass")){
			txt = new Text(0, 0, resourcesManager.font,
					"+10", resourcesManager.vbom);		
		}
		txt.setPosition((width - txt.getWidth()) / 2,
				(height - txt.getHeight()) / 2); 
		mScene.attachChild(txt);
		if(string.equalsIgnoreCase("pass")){
			getBonus(10);			
		}
		ScaleModifier scale = new ScaleModifier(0.5f, 3, 1);
		
		txt.registerEntityModifier(new SequenceEntityModifier(scale));
		PauseableTimerHandler p = new PauseableTimerHandler(1.5f, false, new ITimerCallback() {			
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
			//	txt.clearEntityModifiers(); txt.clearUpdateHandlers();
				if(txt!=null)
				dataString.clearSprite(txt);//txt.setVisible(false);// txt.detachSelf();		
				
				if(txt1!=null)
					dataString.clearSprite(txt1);//txt1.setVisible(false); //txt1.detachSelf();
				if(text!=null){
					dataString.clearSprite(text);//text.setVisible(false);
				} 
				for (TiledSprite t : tiledSpriteText) {
					dataString.clearSprite(t);
				}mScene.resumeTimerHandler(false);
				/*txt1 = new Text(0, 0, resourcesManager.font, "Level2", resourcesManager.vbom);
				txt1.setPosition((width-txt1.getWidth())/2, (height-txt1.getHeight())/2);
				mScene.attachChild(txt1);						
				ScaleModifier scale = new ScaleModifier(0.5f, 0.1f, 2.5f);
				RotationModifier rotate = new RotationModifier(0.25f, 0, 360);
				txt1.registerEntityModifier(new ParallelEntityModifier(scale,rotate));*/
			}
		});
		mScene.registerUpdateHandler(p);
		/*PauseableTimerHandler p1 = new PauseableTimerHandler(2.5f, false, new ITimerCallback() {
			
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				//txt1.clearEntityModifiers(); txt1.clearUpdateHandlers();
				txt1.setVisible(false); //txt1.detachSelf();
				if(text!=null){
				text.setVisible(false);
				}
				for (TiledSprite sprite : tiledSpriteText) {
					sprite.setVisible(false);
				}
				mScene.restartTimerHandler();
			}
		});
		
		mScene.registerUpdateByTimerHandler(p1);*/

		MultiTouchHandler.isTouchIgnore = false;
	}

	private void getBonus(int j) {
		MoveModifier move = new MoveModifier(0.5f, txt.getX(), txt.getY(), 165, 20);
		AlphaModifier alpha = new AlphaModifier(0.1f, 1, 0);
		txt.registerEntityModifier(new SequenceEntityModifier(move,alpha)
		{
			@Override
			protected void onModifierFinished(IEntity pItem) {
				// TODO Auto-generated method stub
				super.onModifierFinished(pItem);
				String score = mScene.score;
				int s = Integer.parseInt(score);
				s += 10;
				mScene.score = s+"";
				Log.d("pos", mScene.score);
				//mScene.text.setText(mScene.score+"");
				mScene.setTextChange();
			}
			
		});		
	}

	public void goLevelNext() {
		mScene.currentLevel++;
		txt1 = new Text(0, 0, resourcesManager.font, "Level"+mScene.currentLevel, resourcesManager.vbom);
		txt1.setPosition((width-txt1.getWidth())/2, (height-txt1.getHeight())/2);
		txt1.setAlpha(0);
		mScene.attachChild(txt1);						
		AlphaModifier alpha = new AlphaModifier(0.5f, 0, 1);
		ScaleModifier scale = new ScaleModifier(0.5f, 0.1f, 2.5f);
		RotationModifier rotate = new RotationModifier(0.25f, 0, 360);
		txt1.registerEntityModifier(new ParallelEntityModifier(scale,rotate,alpha)
		{
			@Override
			protected void onModifierFinished(IEntity pItem) {
				super.onModifierFinished(pItem);
				PauseableTimerHandler p = new PauseableTimerHandler(1f, false, new ITimerCallback() {
					
					@Override
					public void onTimePassed(TimerHandler pTimerHandler) {
						mScene.restartTimerHandler();
					}
				});
				mScene.registerUpdateHandler(p);
			}
			
		});
		
	}

	public void addInnings() {
		MultiTouchHandler.isTouchIgnore = false;
		removeGiftBox();
		TiledSprite tiledSprite = new TiledSprite(0, 0,resourcesManager.mTiledTextureRegionX, resourcesManager.vbom);
		android.graphics.Point p = null;
		switch (mScene.posX) {
		case 1:
			tiledSprite.setWidth(dataString.widthX1);
			tiledSprite.setHeight(dataString.heightX1);
			p = dataString.x1;
			break;
		case 2:
			tiledSprite.setWidth(dataString.widthX2);
			tiledSprite.setHeight(dataString.heightX2);
			p = dataString.x2;
			break;
		case 3:
			tiledSprite.setWidth(dataString.widthX3);
			tiledSprite.setHeight(dataString.heightX3);
			p = dataString.x3; 
			break;
		case 0: 
			p = dataString.x1;
			break;
		} 
		Log.d("pos1", p.x+":"+p.y);
		tiledSprite.setPosition((width-tiledSprite.getWidth())/2,(height-tiledSprite.getHeight())/2);
		mScene.attachChild(tiledSprite);
		final ScaleModifier mod1 = new ScaleModifier(0.5f, 6f, 1f);
		MoveModifier move = new MoveModifier(0.5f, tiledSprite.getX(),  p.x,tiledSprite.getY(), p.y);
		AlphaModifier alpha = new AlphaModifier(0.1f, 1, 0);
		tiledSprite.registerEntityModifier(new SequenceEntityModifier(mod1,move,alpha)
		{
			@Override
			protected void onModifierFinished(IEntity pItem) {
				// TODO Auto-generated method stub
				super.onModifierFinished(pItem);
				mScene.modiferXOnOff(mScene.posX);
				/*for (TiledSprite t : tiledSpriteText) {
					t.clearEntityModifiers();t.clearUpdateHandlers();
					t.setVisible(false);t.detachSelf();
				}*/mScene.resumeTimerHandler(false);
			}
			
		});	
		
	}

}
